﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.DataAccess;
using CSP.Dao.ObjectDaos.CSP;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao.IObjectDaos.CSPBBS;
using CSP.Dao.ObjectDaos.CSPBBS;
using System.Data.Entity.Validation;

namespace CSP.Dao
{
    public class CSPDaoFactory : IDisposable
    {
        private CSPContext _context;
        private bool _isDispose = false;

        /// <summary>
        /// 是否开启事务默认为false 不开启
        /// </summary>
        private bool _isTransaction = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isTransaction"></param>
        public CSPDaoFactory(bool isTransaction = false)
        {
            _context = new CSPContext();
            _isTransaction = isTransaction;
        }

        /// <summary>
        /// 获取工厂实例，并指定是否开启事务
        /// </summary>
        /// <param name="isTransaction">默认为false不开启</param>
        /// <returns></returns>
        public static CSPDaoFactory GetInstance(bool isTransaction = false)
        {
            var factory = new CSPDaoFactory(isTransaction);
            return factory;
        }

        /// <summary>
        /// 获取UserBasicDao实例
        /// </summary>
        /// <returns></returns>
        public IUserBasicDao GetUserBasicDao()
        {
            return new UserBasicDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取AgeGroupDao实例
        /// </summary>
        /// <returns></returns>
        public IAgeGroupDao GetAgeGroupDao()
        {
            return new AgeGroupDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取AgeGroupDao实例
        /// </summary>
        /// <returns></returns>
        public IDomainTypeDao GetDomainTypeDao()
        {
            return new DomainTypeDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取AuthenticationDao实例
        /// </summary>
        /// <returns></returns>
        public IAuthenticationDao GetAuthenticationDao()
        {
            return new AuthenticationDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取联系方式Dao 实例
        /// </summary>
        /// <returns></returns>
        public IContactWayDao GetContactWayDao()
        {
            return new ContactWayDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取新闻实例
        /// </summary>
        /// <returns></returns>
        public INewsDao GetNewsDao()
        {
            return new NewsDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取新闻类型Dao实例
        /// </summary>
        /// <returns></returns>
        public INewsTypeDao GetNewsType()
        {
            return new NewsTypeDao(_context, _isTransaction);
        }


        /// <summary>
        /// 获取订单 Dao实例
        /// </summary>
        /// <returns></returns>
        public IOrderDao GetOrderDao()
        {
            return new OrderDao(_context, _isTransaction);
        }

        public IOrderRemindDao GetOrderRemindDao()
        {
            return new OrderRemindDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取价格单位Dao 实例
        /// </summary>
        /// <returns></returns>
        public IPriceUnitDao GetPriceUnitDao()
        {
            return new PriceUnitDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取搜索缓存Dao 实例
        /// </summary>
        /// <returns></returns>
        public ISearchCacheDao GetSearchCacheDao()
        {
            return new SearchCacheDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取服务区域Dao 实例
        /// </summary>
        /// <returns></returns>
        public IServiceAreaDao GetServiceAreaDao()
        {
            return new ServiceAreaDao(_context, _isTransaction);
        }

        public IServiceAssetDao GetServiceAssetDao()
        {
            return new ServiceAssetDao(_context, _isTransaction);
        }
        ///// <summary>
        ///// 获取服务信息和年龄组的关系Dao 实例
        ///// </summary>
        ///// <returns></returns>
        //public IServiceInfoAgeGroupRelDao GetServiceInfoAgeGroupRelDao()
        //{
        //    return new ServiceInfoAgeGroupRelDao(_context, _isTransaction);
        //}

        /// <summary>
        /// 获取服务信息Dao 实例
        /// </summary>
        /// <returns></returns>
        public IServiceInfoDao GetServiceInfoDao()
        {
            return new ServiceInfoDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取服务信息综合数据列表  实例
        /// </summary>
        /// <returns></returns>
        public ISearchServiceListDao GetSearchServiceListDao()
        {
            return new SearchServiceListDao(_context, _isTransaction);
        }

        ///// <summary>
        ///// 获取服务信息与服务范围关系Dao 实例
        ///// </summary>
        ///// <returns></returns>
        //public IServiceInfoServiceAreaRelDao GetServiceInfoServiceAreaRelDao()
        //{
        //    return new ServiceInfoServiceAreaRelDao(_context, _isTransaction);
        //}

        ///// <summary>
        ///// 获取服务信息与服务类型关系Dao 实例
        ///// </summary>
        ///// <returns></returns>
        //public IServiceInfoServiceTypeRelDao GetServiceInfoServiceTypeRelDao()
        //{
        //    return new ServiceInfoServiceTypeRelDao(_context, _isTransaction);
        //}

        /// <summary>
        /// 获取服务类型Dao 实例
        /// </summary>
        /// <returns></returns>
        public IServiceTypeDao GetServiceTypeDao()
        {
            return new ServiceTypeDao(_context, _isTransaction);
        }

        /// <summary>
        /// 用户信誉Dao 实例
        /// </summary>
        /// <returns></returns>
        public IUserCreditDao GetUserCreditDao()
        {
            return new UserCreditDao(_context, _isTransaction);
        }

        public IServiceImageDao GetServiceImageDao() 
        {
            return new ServiceImageDao(_context,_isTransaction);
        }

        /// <summary>
        /// 获取评价明细实例
        /// </summary>
        /// <returns></returns>
        public ICreditLineDao GetCreditLineDao()
        {
            return new CreditLineDao(_context, _isTransaction);
        }

        public IClassTypeDao GetClassTypeDao()
        {
            return new ClassTypeDao(_context, _isTransaction);
        }

        /// <summary>
        /// 用户地址信息实例
        /// </summary>
        /// <returns></returns>
        public IAddressDao GetAddressDao()
        {
            return new AddressDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取推荐数据
        /// </summary>
        /// <returns></returns>
        public IRecommendDao GetRecommendDao()
        {
            return new RecommendDao(_context, _isTransaction);
        }


        public IRateStatisticDao GetRateStatisticDao()
        {
            return new RateStatisticDao(_context, _isTransaction);
        }

        public ISearchInfoDao GetSearchInfoDao()
        {
            return new SearchInfoDao(_context, _isTransaction);
        }

        /// <summary>
        /// 获取搜集商家数据
        /// </summary>
        /// <returns></returns>
        public ICollectBusinessInfoDao GetCollectBusinessInfoDao()
        {
            return new CollectBusinessInfoDao(_context, _isTransaction);
        }

        public IMemberDao GetMemberDao()
        {
            return new MemberDao();
        }

        public IMemberProfileDao GetMemberProfileDao()
        {
            return new MemberProfileDao();
        }

        public ICommonMemberDao GetCommonMemberDao()
        {
            return new CommonMemberDao();
        }

        public IDengLuBindInfoDao GetDengLuBindInfoDao()
        {
            return new DengluBindInfoDao();
        }

    


        ~CSPDaoFactory()
        {
            try
            {
                DaoDispose();
            }
            catch (DbEntityValidationException dbEx)
            {
                throw dbEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                _isDispose = true;
                this.Dispose();
            }
        }

        /// <summary>
        /// 事务提交
        /// </summary>
        public void TransactionCommit()
        {
            _context.TransactionCommit();
        }

        public void Dispose()
        {
            DaoDispose();
        }

        /// <summary>
        /// 自定义释放方法
        /// </summary>
        public virtual void DaoDispose()
        {
            //如果已经执行过，不再执行
            if (_isDispose)
            {
                return;
            }
            //资源释放时处理提交改变。
            _context.TransactionCommit();

            _isDispose = true;
        }
    }
}
